package com.cbtx.module.common.utils;

import android.os.Environment;
import android.text.TextUtils;

import com.cbtx.module.common.bean.GoodsEntity;
import com.txcb.lib.base.utils.Base64Util;
import com.txcb.lib.base.utils.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : Lambert
 * date   : 2020/10/28 10:52 AM
 * 数据缓存
 */
public class DataCacheUtil {

    /**
     * 缓存收藏宝首页数据
     *
     * @param
     * @param datas
     */
    public static void saveGoodsTreasureList(List<GoodsEntity.DataBean.ListBean> datas) {

        if (datas == null) {
            return;
        }

        File file = new File(FileUtils.getCacheDir() + "/" + "GoodsTreasureList.json");
        if (file.exists()) {
            file.delete();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String base64 = Base64Util.objectTo64Str(datas);
        saveToSDCard(file, base64);
    }

    /**
     * 获取收藏宝首页数据
     *
     * @param
     * @param
     */
    public static List<GoodsEntity.DataBean.ListBean> readGoodsTreasureList() {
        List<GoodsEntity.DataBean.ListBean> mDatas = new ArrayList<>();
        File file = new File(FileUtils.getCacheDir() + "/" + "GoodsTreasureList.json");
        if (!file.exists()) {
            file.mkdir();
        }

        String base64 = readTextFile(file.getPath());

        if (TextUtils.isEmpty(base64)) {
            return mDatas;
        }

        try {
            mDatas.addAll((List<GoodsEntity.DataBean.ListBean>) Base64Util.base64ToObject(base64));
        } catch (Exception e) {

        }


        return mDatas;
    }


    /**
     * 缓存认证首页数据
     *
     * @param
     * @param datas
     */
    public static void saveGoodsAuthList(List<GoodsEntity.DataBean.ListBean> datas) {

        if (datas == null) {
            return;
        }

        File file = new File(FileUtils.getCacheDir() + "/" + "GoodsAuthList.json");
        if (file.exists()) {
            file.delete();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String base64 = Base64Util.objectTo64Str(datas);
        saveToSDCard(file, base64);
    }

    public static List<GoodsEntity.DataBean.ListBean> readGoodsAuthList() {
        List<GoodsEntity.DataBean.ListBean> mDatas = new ArrayList<>();
        File file = new File(FileUtils.getCacheDir() + "/" + "GoodsAuthList.json");
        if (!file.exists()) {
            file.mkdir();
        }

        String base64 = readTextFile(file.getPath());

        if (TextUtils.isEmpty(base64)) {
            return mDatas;
        }

        try {
            mDatas.addAll((List<GoodsEntity.DataBean.ListBean>) Base64Util.base64ToObject(base64));
        } catch (Exception e) {

        }


        return mDatas;
    }


    /**
     * 保存收藏宝详情数据
     *
     * @param mData
     */
    public static void saveGoodsTreasureDetail(GoodsEntity.DataBean.ListBean mData) {
        File file = new File(FileUtils.getCacheDir(), "GoodsTreasureList");
        if (!file.exists()) {
            file.mkdir();
        }


        File file2 = new File(file.getAbsolutePath(), "GoodsTreasure" + mData.serialNumber);
        if (file2.exists()) {
            file2.delete();
        }

        try {
            file2.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        String json = new Gson().toJson(mData);

        String base64 = Base64Util.objectTo64Str(mData);
        saveToSDCard(file2, base64);
    }

    /**
     * 获取收藏宝详情数据
     *
     * @param serialNumber
     * @return
     */
    public static GoodsEntity.DataBean.ListBean getGoodsTreasureDetail(String serialNumber) {
        GoodsEntity.DataBean.ListBean renZhengItem = new GoodsEntity.DataBean.ListBean();

        File file = new File(FileUtils.getCacheDir(), "GoodsTreasureList");
        if (!file.exists()) {
            file.mkdir();
        }


        File file2 = new File(file.getAbsolutePath(), "GoodsTreasure" + serialNumber);


        String base64 = readTextFile(file2.getPath());

        if (TextUtils.isEmpty(base64)) {
            return renZhengItem;
        }

        try {
            renZhengItem = (GoodsEntity.DataBean.ListBean) Base64Util.base64ToObject(base64);
        } catch (Exception e) {

        }


        return renZhengItem;

    }


    //-----------------------------------------------------------------

    public static void saveToSDCard(File file, String content) {
        String en = Environment.getExternalStorageState();
        if (!file.exists()) {
            file.mkdir();
        }
        //获取SDCard状态,如果SDCard插入了手机且为非写保护状态
        if (en.equals(Environment.MEDIA_MOUNTED)) {
            try {
                OutputStream out = new FileOutputStream(file);
                out.write(content.getBytes());
                out.close();

            } catch (Exception e) {
                e.printStackTrace();

            }
        } else {
            //提示用户SDCard不存在或者为写保护状态

        }
    }

    /**
     * 从本地读取json
     */
    public static String readTextFile(String filePath) {
        StringBuilder sb = new StringBuilder();
        try {
            File file = new File(filePath);
            InputStream in = null;
            in = new FileInputStream(file);
            int tempbyte;
            while ((tempbyte = in.read()) != -1) {
                sb.append((char) tempbyte);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
